/// <reference path="prototype.js" />
/// <reference path="GameBoard.class.js" />
/// <reference path="Snake.class.js" />
/// <reference path="GameController.class.js" />

var SnakeGame = Class.create({

    initialize: function(boardDiv, playerlistDiv, cols, rows, nickname) {

				if( typeof window.console == "undefined")
					window.console = false;

        var board = new GameBoard( boardDiv, cols, rows, this.calculateCellSize(cols, rows) );
        new GameController(board, playerlistDiv, nickname);

    },

    calculateCellSize: function(cols, rows) {
    
        var spaceToKeepOnTheRight = 200;
        var bdy = $(document.body);
        var w = document.viewport.getWidth() - 2 - spaceToKeepOnTheRight - parseInt(bdy.getStyle("margin-left")) - parseInt(bdy.getStyle("margin-right"));
        var h = document.viewport.getHeight() - 2 - parseInt(bdy.getStyle("margin-top")) - parseInt(bdy.getStyle("margin-bottom"));

        //find the first cellSize that fits the screen
        //we start with a cellSize of 20 pixels and decrement it until it fits the screen
        var cellSize = 20;
        while (cellSize * cols > w || cellSize * rows > h)
            cellSize--;

        if (!cellSize > 0) {
            alert("Error while trying to figure out screen size");
            return;
        }

        return cellSize;

    }

});
